package com.example.demo.service.impl;

import com.example.demo.dao.PetMapper;
import com.example.demo.entity.PO.PetPO;
import com.example.demo.entity.VO.pet.PetVO;
import com.example.demo.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PetServiceImpl implements PetService {

    private final PetMapper petMapper;


    @Autowired
    public PetServiceImpl(PetMapper petMapper) {
        this.petMapper = petMapper;
    }

    @Override
    public PetVO getPetByUserId(Integer userId) {
        PetPO petPO = petMapper.getPetByUserId(userId);

        return PetVO.builder()
                .id(petPO.getId())
                .userId(petPO.getUserId())
                .name(petPO.getName())
                .hunger(petPO.getHunger())
                .cleanliness(petPO.getCleanliness())
                .mood(petPO.getMood())
                .level(petPO.getLevel())
                .build();
    }

    @Override
    public void createPet(Integer userId) {
        PetPO petPO = PetPO.builder()
                .userId(userId)
                .name("宠物")
                .hunger(100)
                .cleanliness(100)
                .mood(100)
                .level(1)
                .createdAt(System.currentTimeMillis())
                .updatedAt(System.currentTimeMillis())
                .build();

        petMapper.savePet(petPO);
    }

    @Override
    public void updatePet(PetVO petVO) {
        PetPO petPO = PetPO.builder()
                .id(petVO.getId())
                .name(petVO.getName())
                .hunger(petVO.getHunger())
                .cleanliness(petVO.getCleanliness())
                .mood(petVO.getMood())
                .level(petVO.getLevel())
                .updatedAt(System.currentTimeMillis())
                .build();

        petMapper.updatePet(petPO);
    }
}
